package com.pureut.storage.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.pureut.storage.dto.PickingRestDbDto;
import com.pureut.storage.dto.PickingRestHandoverDto;
import com.pureut.storage.dto.PickingReturnsDto;
import com.pureut.storage.export.PickingReturnsExport;
import com.pureut.storage.service.PickingReturnsService;
import com.pureut.storage.util.ExcelUtil;
import com.pureut.storage.vo.OrderReviewVo;
import com.pureut.storage.vo.PickingRestDbVo;
import com.pureut.storage.vo.PickingReturnsVo;
import com.pureut.storage.vo.UpdateDocVo;
import com.sale.common.core.domain.R;
import com.sale.common.core.web.controller.BaseController;
import com.sale.common.log.annotation.Log;
import com.sale.common.log.enums.BusinessType;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 委外退货
 * @author: CM
 * @date: 2022/12/30  16:34
 */
@RestController
@RequestMapping("/pickingReturns")
public class PickingReturnsController extends BaseController {

    @Resource
    PickingReturnsService pickingReturnsService;

    /**
     * 新增委外退货单
     */
    @SaCheckPermission("warehouse:outbound:subcontout:add")
    @Log(title = "新增委外退货单", businessType = BusinessType.INSERT)
    @PostMapping("/addReturns")
    public R<Void> addReturns(@RequestBody PickingReturnsVo pickingReturnsVo) throws Exception {
        return toAjax(pickingReturnsService.addReturns(pickingReturnsVo));
    }

    /**
     * 关结委外退货单
     */
    @SaCheckPermission("warehouse:outbound:subcontout:close")
    @Log(title = "关结委外退货单", businessType = BusinessType.UPDATE)
    @PostMapping("/closeReturns")
    public R<Void> closeReturns(@RequestBody PickingReturnsVo pickingReturnsVo) throws Exception {
        return toAjax(pickingReturnsService.closeReturns(pickingReturnsVo));
    }

    /**
     * 提交审核
     */
    @SaCheckPermission("warehouse:outbound:subcontout:submit")
    @Log(title = "提交审核", businessType = BusinessType.UPDATE)
    @GetMapping("/submitReturns/{ids}")
    public R<Void> submitReturns(@PathVariable("ids") String ids) throws Exception {
        return toAjax(pickingReturnsService.submitReturns(ids));
    }

    /**
     * 审核单据
     */
    @SaCheckPermission("warehouse:outbound:subcontout:examine")
    @Log(title = "审核单据", businessType = BusinessType.UPDATE)
    @PutMapping("/returnReview")
    public R<Void> returnReview(@Validated @RequestBody OrderReviewVo orderReviewVo) throws Exception {
        return toAjax(pickingReturnsService.returnReview(orderReviewVo));
    }

    /**
     * 删除单据
     */
    @SaCheckPermission("warehouse:outbound:subcontout:remove")
    @Log(title = "删除单据", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteReturns/{ids}")
    public R<Void> deleteReturns(@PathVariable("ids") String ids) throws Exception {
        return toAjax(pickingReturnsService.deleteReturns(ids));
    }

    /**
     * 修改委外退货单
     */
    @SaCheckPermission("warehouse:outbound:subcontout:edit")
    @Log(title = "修改采购退货单", businessType = BusinessType.UPDATE)
    @PostMapping("/updateReturns")
    public R<Void> updateReturns(@RequestBody PickingReturnsVo pickingReturnsVo) throws Exception {
        return toAjax(pickingReturnsService.updateReturns(pickingReturnsVo));
    }

    /**
     * 查看委外退货单
     */
    @SaCheckPermission("warehouse:outbound:subcontout:query")
    @GetMapping("/view/{id}")
    public R<PickingReturnsDto> view(@PathVariable("id") Long id) throws Exception {
        return R.ok(pickingReturnsService.view(id));
    }

    /**
     * 查看委外退货列表
     */
    @GetMapping("/list")
    public TableDataInfo<PickingReturnsDto> getList(PickingReturnsVo pickingReturnsVo, PageQuery pageQuery) throws Exception {
        //查询销售发货列表
        return pickingReturnsService.getList(pickingReturnsVo, pageQuery);
    }

    /**
     * 导出
     */
    @PostMapping("/export")
    @SaCheckPermission("warehouse:outbound:subcontout:export")
    public R<Void> inspectionMessageExport(HttpServletResponse response,PickingReturnsVo pickingReturnsVo) throws IOException {
        List<PickingReturnsExport> infoExport = pickingReturnsService.getInfoExport(pickingReturnsVo);
        ExcelUtil<PickingReturnsExport> excelUtil = new ExcelUtil<PickingReturnsExport>(PickingReturnsExport.class);
        excelUtil.excelWriter(response, infoExport, "采购退货信息.xlsx");
        return R.ok();
    }

    /**
     * 委外退货（pda）
     */
    @SaCheckPermission("warehouse:outbound:subcontout:outbound")
    @PostMapping("/outReturns")
    @Log(title = "委外退货(pda)", businessType = BusinessType.UPDATE)
    public R<PickingRestDbDto> outReturns(@RequestBody PickingRestDbVo pickingRestDbVo) throws Exception {
        return R.ok(pickingReturnsService.outReturns(pickingRestDbVo));
    }

    /**
     * 拣货完成
     */
    @GetMapping("/subcontractingPicking/{id}")
    @Log(title = "委外退货拣货完成", businessType = BusinessType.UPDATE)
    public R<Void> subcontractingPicking(@PathVariable("id")  Long id) throws Exception {
        return toAjax(pickingReturnsService.subcontractingPicking(id));
    }

    /**
     * 委外退货交接
     */
    @SaCheckPermission("warehouse:outbound:subcontout:handover")
    @PostMapping("/overProductionReturns")
    @Log(title = "委外退货交接", businessType = BusinessType.UPDATE)
    public R<PickingRestHandoverDto> overProductionReturns(@RequestBody PickingRestDbVo pickingRestDbVo) {
        return R.ok(pickingReturnsService.overProductionReturns(pickingRestDbVo));
    }

    /**
     * 修改检验单状态Feign
     */
    @Log(title = "修改单据", businessType = BusinessType.UPDATE)
    @PostMapping("/returnUpdateStatus")
    public R<Void> returnUpdateStatus(@RequestBody UpdateDocVo updateDocVo) throws Exception {
        return toAjax(pickingReturnsService.returnUpdateStatus(updateDocVo));
    }


}
